package com.bsj.power.pls.web;

import com.alibaba.excel.EasyExcelFactory;
import com.bsj.power.common.config.exception.ApiException;
import com.bsj.power.common.config.global.JsonResult;
import com.bsj.power.common.config.global.JsonResultEnum;
import com.bsj.power.common.def.dto.alarmReport.AlarmHandleDTO;
import com.bsj.power.common.def.dto.alarmReport.AlarmReportSearchDTO;
import com.bsj.power.common.def.vo.pls.AlarmReportCountVO;
import com.bsj.power.common.def.vo.pls.AlarmReportVO;
import com.bsj.power.common.def.vo.pls.alarm.AlarmDurationVO;
import com.bsj.power.common.def.vo.pls.excel.AlarmReportCountExcelVO;
import com.bsj.power.common.def.vo.pls.excel.AlarmReportExcelVO;
import com.bsj.power.common.def.vo.pls.excel.ComAlarmExcelVO;
import com.bsj.power.common.util.DateUtils;
import com.bsj.power.pls.service.AlarmReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author LiJiaXin
 * @version 1.0.0
 * @description AlarmReportController
 * @time 2024/4/25 17:12
 */
@RestController
@RequestMapping("/alarmReport")
public class AlarmReportController {

    @Autowired
    private AlarmReportService alarmReportService;

    /**
     * 报警报表
     *
     * @param alarmReportSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.AlarmReportCountVO>>
     * @author ljx
     * @time 2024/4/25 17:31
     */
    @GetMapping("/page")
    public JsonResult<List<AlarmReportCountVO>> page(@Validated AlarmReportSearchDTO alarmReportSearchDTO) {
        return alarmReportService.pageAlarmReportCount(alarmReportSearchDTO);
    }

    /**
     * 报警详情
     *
     * @param alarmReportSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.AlarmReportVO>>
     * @author ljx
     * @time 2024/5/13 9:59
     */
    @GetMapping("/detail")
    public JsonResult<List<AlarmReportVO>> alarmReportDetail(@Validated AlarmReportSearchDTO alarmReportSearchDTO) {
        return alarmReportService.alarmReportDetail(alarmReportSearchDTO);
    }

    /**
     * 报警区域统计
     *
     * @param startTime
     * @param endTime
     * @return com.bsj.power.common.config.global.JsonResult<java.util.Map < java.lang.String, java.lang.Long>>
     * @author ljx
     * @time 2024/5/13 15:49
     */
    @GetMapping("/areaCount")
    public JsonResult<Map<String, Long>> alarmAreaCount(String startTime, String endTime) throws ParseException {
        return JsonResult.success(alarmReportService.alarmAreaCount(startTime, endTime));
    }

    /**
     * 报警次数趋势
     *
     * @param startTime
     * @param endTime
     * @param type      1单位月 2单位日
     * @return com.bsj.power.common.config.global.JsonResult<java.util.Map < java.lang.String, java.lang.Long>>
     * @author ljx
     * @time 2024/5/13 16:10
     */
    @GetMapping("/timeCount")
    public JsonResult<Map<String, Long>> alarmTimeCount(String startTime, String endTime, Integer type) throws ParseException {
        return JsonResult.success(alarmReportService.alarmTimeCount(startTime, endTime, type));
    }

    /**
     * 按时间统计时长
     *
     * @param startTime
     * @param endTime
     * @return
     * @throws ParseException
     */
    @GetMapping("/timeDuration")
    public JsonResult<List<AlarmDurationVO>> alarmDurationCount(String startTime, String endTime) throws ParseException {
        return JsonResult.success(alarmReportService.alarmDurationCount(startTime, endTime));
    }

    /**
     * 报警信息导出
     *
     * @param response
     * @param alarmReportSearchDTO
     * @return void
     * @author ljx
     * @time 2024/5/14 11:53
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response, AlarmReportSearchDTO alarmReportSearchDTO) throws IOException {
        List<AlarmReportCountExcelVO> alarmReportCountExcelVOS = alarmReportService.getExportData(alarmReportSearchDTO);
        if (CollectionUtils.isEmpty(alarmReportCountExcelVOS)) {
            throw new ApiException(JsonResultEnum.NO_EXPORT_DATA);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("报警统计信息" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), AlarmReportCountExcelVO.class).sheet("报警统计信息")
                .doWrite(alarmReportCountExcelVOS);
    }

    /**
     * 导出报警详情
     *
     * @param response
     * @param alarmReportSearchDTO
     * @return void
     * @author ljx
     * @time 2024/9/5 11:50
     */
    @GetMapping("/exportDetail")
    public void exportDetail(HttpServletResponse response, AlarmReportSearchDTO alarmReportSearchDTO) throws IOException {
        List<AlarmReportExcelVO> alarmReportExcelVOS = alarmReportService.getExportDetail(alarmReportSearchDTO);
        if (CollectionUtils.isEmpty(alarmReportExcelVOS)) {
            throw new ApiException(JsonResultEnum.NO_EXPORT_DATA);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("报警信息" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), AlarmReportExcelVO.class).sheet("报警信息")
                .doWrite(alarmReportExcelVOS);
    }

    /**
     * 处理报警
     *
     * @param alarmHandleDTO
     * @return com.bsj.power.common.config.global.JsonResult
     * @author ljx
     * @time 2024/5/23 16:02
     */
    @PostMapping("/handleAlarm")
    public JsonResult handleAlarm(@RequestBody @Validated AlarmHandleDTO alarmHandleDTO) {
        return alarmReportService.handleAlarm(alarmHandleDTO);
    }

    /**
     * 组合报警分页
     *
     * @param alarmReportSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < com.bsj.power.common.def.vo.pls.AlarmReportVO>>
     * @author ljx
     * @time 2024/10/12 9:33
     */
    @GetMapping("/pageComAlarm")
    public JsonResult<List<AlarmReportVO>> pageComAlarm(@Validated AlarmReportSearchDTO alarmReportSearchDTO) {
        return alarmReportService.pageComAlarm(alarmReportSearchDTO);
    }

    /**
     * 导出组合报警信息
     *
     * @param response
     * @param alarmReportSearchDTO
     * @return void
     * @author ljx
     * @time 2024/10/12 10:30
     */
    @GetMapping("/exportComAlarm")
    public void exportComAlarm(HttpServletResponse response, AlarmReportSearchDTO alarmReportSearchDTO) throws IOException {
        List<ComAlarmExcelVO> comAlarmExcelVOS = alarmReportService.getExportComAlarmInfo(alarmReportSearchDTO);
        if (CollectionUtils.isEmpty(comAlarmExcelVOS)) {
            throw new ApiException(JsonResultEnum.NO_EXPORT_DATA);
        }
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //编码 防止中文乱码
        String fileName = URLEncoder.encode("组合报警信息" + DateUtils.dateToStr(DateUtils.TIME_STANDARD_FORMAT2, new Date()), "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        EasyExcelFactory.write(response.getOutputStream(), ComAlarmExcelVO.class).sheet("组合报警信息")
                .doWrite(comAlarmExcelVOS);
    }

    /**
     * 根据设备号查询未结束的报警类型
     *
     * @param deviceName
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < java.lang.Integer>>
     * @author ljx
     * @time 2024/10/24 11:27
     */
    @GetMapping("/getAlarmTypeByDeviceName/{deviceName}")
    public JsonResult<List<Integer>> getAlarmTypeByDeviceName(@PathVariable("deviceName") String deviceName) {
        return JsonResult.success(alarmReportService.getAlarmTypeByDeviceName(deviceName));
    }

    /**
     * uwb报警列表
     * 查询轨迹时，以同样的参数查询UWB设备产生的报警
     *
     * @param alarmReportSearchDTO
     * @return com.bsj.power.common.config.global.JsonResult<java.util.List < java.lang.Integer>>
     * @author ljx
     * @time 2024/11/1 10:22
     */
    @GetMapping("/listUwbAlarm")
    public JsonResult<List<AlarmReportVO>> listUwbAlarm(AlarmReportSearchDTO alarmReportSearchDTO) {
        return JsonResult.success(alarmReportService.listUwbAlarm(alarmReportSearchDTO));
    }

}
